package com.hdkj.educenter.controller;


import com.hdkj.commonutils.JwtUtils;
import com.hdkj.commonutils.R;
import com.hdkj.commonutils.ordervo.UcenterMemberOrder;
import com.hdkj.educenter.client.EduTimeStampClient;
import com.hdkj.educenter.entity.UcenterMember;
import com.hdkj.educenter.entity.vo.ChangeVo;
import com.hdkj.educenter.entity.vo.RegisterVo;
import com.hdkj.educenter.entity.vo.timestampVo;
import com.hdkj.educenter.service.UcenterMemberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author sx
 * @since 2022-5-22
 */
@RestController
@RequestMapping("/educenter/member")
//@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    @Autowired
    private EduTimeStampClient eduTimeStampClient;

    //登录
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member) {
        //member对象封装手机号和密码
        //调用service方法实现登录
        //返回token值，使用jwt生成
        String token = memberService.login(member);
        return R.ok().data("token",token);
    }

    //注册
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo,HttpServletRequest request) {
        String uid= JwtUtils.getMemberIdByJwtToken(request);
        boolean flag = memberService.register(registerVo);
        timestampVo timestampVo = new timestampVo();
        timestampVo.setUserid(uid);
        timestampVo.setContent("这是您第一次走进信工开源的世界！");
        timestampVo.setTitle("第一次注册");
        timestampVo.setTime(new Date());
        eduTimeStampClient.saveTs(timestampVo);
        return R.ok().data("msg",flag);
    }


    //更改密码
    @ApiOperation(value = "更改密码")
    @PostMapping("change")
    public R changePassword(@RequestBody ChangeVo changeVo) {
        String changePasswd = memberService.changePasswd(changeVo);
        return R.ok().data("changePasswd",changePasswd);
    }

    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }

    //根据用户id获取用户信息
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id) {
        UcenterMember member = memberService.getById(id);
        //把member对象里面值复制给UcenterMemberOrder对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    //根据用户id获取用户信息
    @PostMapping("getUserInfoOrders/{ids}")
    public List<UcenterMemberOrder> getUserInfoOrders(@PathVariable List<String> ids) {
        List<UcenterMember> ucenterMembers = memberService.listByIds(ids);
        List<UcenterMemberOrder> ucenterMemberOrders=new ArrayList<>();
        //把member对象里面值复制给UcenterMemberOrder对象
        for (UcenterMember ucenterMember : ucenterMembers) {
            UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
            BeanUtils.copyProperties(ucenterMember,ucenterMemberOrder);
            ucenterMemberOrders.add(ucenterMemberOrder);
        }
        return ucenterMemberOrders;
    }

    //查询某一天注册人数
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day) {
        Integer count = memberService.countRegisterDay(day);
        return R.ok().data("countRegister",count);
    }


    //根据用户id获取用户信息
    @ApiOperation(value = "根据用户id获取用户信息 个人中心用")
    @PostMapping("getUserInfo/{id}")
    public R getUserInfo(@PathVariable String id) {
        //  public UcenterMemberOrder getUserInfoOrder(@PathVariable String id) {
        UcenterMember member = memberService.getById(id);
        //把member对象里面值复制给UcenterMemberOrder对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        //       return ucenterMemberOrder;
        return R.ok().data("memberInfo",ucenterMemberOrder);
    }

    //用户信息修改功能
    @ApiOperation(value = "用户信息修改")
    @PostMapping("updateMember")
    public R updateMember(@RequestBody UcenterMember ucenterMember,HttpServletRequest request) {
        String uid = JwtUtils.getMemberIdByJwtToken(request);
        boolean flag = memberService.updateById(ucenterMember);

        if(flag) {

            timestampVo timestampVo = new timestampVo();

            R res = eduTimeStampClient.getinfo("完善个人信息",uid);
            System.err.println(res.getSuccess());
            if (res.getSuccess()){
                timestampVo.setTime(new Date());
                timestampVo.setUserid(ucenterMember.getId());
                timestampVo.setTitle("完善个人信息");
                timestampVo.setContent("您第一次向我们展现更完美的自己,未来一定是属于您的");
                eduTimeStampClient.saveTs(timestampVo);
            }
            return R.ok();
        } else {
            return R.error();
        }
    }


}

